<?php
namespace App\V3\Api\Services;

use App\V3\Api\Models\AdminModel;
use App\V3\Api\Models\StorageModel;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class MessageService {
  /**
     * 出入库存微信推送
     * 主账号以及仓库负责人可接收该消息提醒
     * @param $info 物料库存信息
     * @param $data 操作数据信息
     * @param $i 标示：0是入库、1是出库  
     */
    static public function sendWxInOrOuStore($info,$i,$data)
    {
        //$mainInfo = AdminModel::where('id', $data['main_id'])->first();
        
        if($data['main_id'] == $data['user_id']){
            $userInfo = [];
            $mainInfo = AdminModel::where('id', $data['main_id'])->first();
        
        }else{
            $mainInfo = AdminModel::where('id', $data['main_id'])->first();
            $userInfo = AdminModel::where('id', $data['user_id'])->first();
        }
        
        $sInfo = StorageModel::select(
            'mobile',
            'openid',
            'device_id'
            )
            ->from('storage as a')
            ->leftJoin('admin as b', 'b.id', '=', 'a.user_id')
            ->where('a.id', $info['storage_id'])
            ->first();
        
            if($i==0){//入库推送
                
                if($mainInfo['device_id'] == $sInfo['device_id']) {
                    if(!empty($adminInfo['device_id'])) {
                        //个推-主账号
                        GeTuiService::push(
                            '入库提醒',
                            '商品：'.$materielName.'-'.
                            $typeName.'已成功入库至仓库:'.$storageName.'，当前剩余库存数：'.
                            $inStorageNum.'入库操作数量:'.$num,
                            $mainInfo['device_id'],
                            );
                    }
                
                    if(!empty($userInfo['device_id'])) {
                        //个推-操作人
                        GeTuiService::push(
                            '入库提醒',
                            '商品：'.$materielName.'-'.
                            $typeName.'已成功入库至仓库:'.$storageName.'，当前剩余库存数：'.
                            $inStorageNum.'入库操作数量:'.$num,
                            $userInfo['device_id'],
                            );
                    }
                }else {
                    if(!empty($mainInfo['device_id'])) {
                        //个推-主账号
                        GeTuiService::push(
                            '入库提醒',
                            '商品：'.$materielName.'-'.
                            $typeName.'已成功入库至仓库:'.$storageName.'，当前剩余库存数：'.
                            $inStorageNum.'入库操作数量:'.$num,
                            $mainInfo['device_id'],
                            );
                    }
                
                    if( !empty($userInfo) && $userInfo['device_id'] == $sInfo['device_id']){
                        if(!empty($userInfo['device_id'])) {
                            //个推-操作人
                            GeTuiService::push(
                                '入库提醒',
                                '商品：'.$materielName.'-'.
                                $typeName.'已成功入库至仓库:'.$storageName.'，当前剩余库存数：'.
                                $inStorageNum.'入库操作数量:'.$num,
                                $userInfo['device_id'],
                                );
                        }
                    }else{
                        if(!empty($sInfo['device_id'])) {
                            //个推-主账号
                            GeTuiService::push(
                                '入库提醒',
                                '商品：'.$materielName.'-'.
                                $typeName.'已成功入库至仓库:'.$storageName.'，当前剩余库存数：'.
                                $inStorageNum.'入库操作数量:'.$num,
                                $sInfo['device_id'],
                                );
                        }
                
                        if(!empty($userInfo['device_id'])) {
                            //个推-操作人
                            GeTuiService::push(
                                '入库提醒',
                                '商品：'.$materielName.'-'.
                                $typeName.'已成功入库至仓库:'.$storageName.'，当前剩余库存数：'.
                                $inStorageNum.'入库操作数量:'.$num,
                                $userInfo['device_id'],
                                );
                        }
                    }
                }
                                
                if(empty($info['purchase_no'])){
                    $info['purchase_no']=time();
                }
                $msgContent = [
                    'appid' => config('app.wechat_app_id'),
                    'template_id' => config('app.in_tmpl'),
                    'url' => '',
                    'miniprogram' => '',
                    'data' => [
                        'first' => ['value' => '已入库完成'],
                        'keyword1' => ['value' => $info['purchase_no']],
                        'keyword2' => ['value' => $info['storage_name']],
                        'keyword3' => ['value' => $info['materiel_name'].'-'.
                            $info['type_name']],
                        'keyword4' => ['value' => $data['num']],
                        'keyword5' => ['value' => date('Y-m-d H:i:s')],
                        'remark' => ['value' => '']
                    ]
                ];
            
                if($mainInfo['openid'] == $sInfo['openid']) {
                    if(!empty($mainInfo['openid'])) {
                        //操作人接收消息
                        SELF::pushMsg(
                            $mainInfo['openid'],
                            $msgContent
                            );
                    }
                }
                else {
                    if(!empty($mainInfo['openid'])) {
                        //操作人接收消息
                        SELF::pushMsg(
                            $mainInfo['openid'],
                            $msgContent
                            );
                    }
            
                    if(!empty($sInfo['openid'])) {
                        //仓库负责人接收消息
                        SELF::pushMsg(
                            $sInfo['openid'],
                            $msgContent
                            );
                    }
                }
            
                return true;
            }

            if($i==1 && $info['in_storage_num'] - $data['num'] == 0){//出库时，库存为0时，推送出库不足
               
                if($mainInfo['device_id'] == $sInfo['device_id']) {
                    if(!empty($mainInfo['device_id'])) {
                        //个推-主账号
                        GeTuiService::push(
                            '库存不足',
                            '仓库：'.$storageName.
                            '中商品：'.$materielName.'-'.
                            $typeName.'剩余库存数不足，当前剩余库存数：'.
                            $inStorageNum.'为避免影响使用，请及时补充库存',
                            $mainInfo['device_id'],
                            );
                    }
                
                    if(!empty($userInfo['device_id'])){
                        //个推-操作
                        GeTuiService::push(
                            '库存不足',
                            '仓库：'.$storageName.
                            '中商品：'.$materielName.'-'.
                            $typeName.'剩余库存数不足，当前剩余库存数：'.
                            $inStorageNum.'为避免影响使用，请及时补充库存',
                            $userInfo['device_id'],
                            );
                    }
                }else {
                    if(!empty($mainInfo['device_id'])) {
                        //个推-主账号
                        GeTuiService::push(
                            '库存不足',
                            '仓库：'.$storageName.
                            '中商品：'.$materielName.'-'.
                            $typeName.'剩余库存数不足，当前剩余库存数：'.
                            $inStorageNum.'为避免影响使用，请及时补充库存',
                            $mainInfo['device_id'],
                            );
                    }
                
                    if( !empty($userInfo) && $userInfo['device_id'] == $sInfo['device_id']){
                
                        if(!empty($userInfo['device_id'])){
                            //个推-操作
                            GeTuiService::push(
                                '库存不足',
                                '仓库：'.$storageName.
                                '中商品：'.$materielName.'-'.
                                $typeName.'剩余库存数不足，当前剩余库存数：'.
                                $inStorageNum.'为避免影响使用，请及时补充库存',
                                $userInfo['device_id'],
                                );
                        }
                    }else{
                
                        if(!empty($storageManagerInfo['device_id'])) {
                            //个推-仓库负责人
                            GeTuiService::push(
                                '库存不足',
                                '仓库：'.$storageName.
                                '中商品：'.$materielName.'-'.
                                $typeName.'剩余库存数不足，当前剩余库存数：'.
                                $inStorageNum.'为避免影响使用，请及时补充库存',
                                $sInfo['device_id'],
                                );
                        }
                
                        if(!empty($userInfo['device_id'])){
                            //个推-操作
                            GeTuiService::push(
                                '库存不足',
                                '仓库：'.$storageName.
                                '中商品：'.$materielName.'-'.
                                $typeName.'剩余库存数不足，当前剩余库存数：'.
                                $inStorageNum.'为避免影响使用，请及时补充库存',
                                $userInfo['device_id'],
                                );
                        }
                    }
                }
                
                $msgContent = [
                        'appid' => config('app.wechat_app_id'),
                        'template_id' => config('app.short_tmpl'),
                        'url' => '',
                        'miniprogram' => '',
                        'data' => [
                            'first' => ['value' => '库存不足'],
                            'keyword1' => ['value' => $info['materiel_name']],
                            'keyword2' => ['value' => $info['materiel_name']],
                            'keyword3' => ['value' => $info['in_storage_num'] - $data['num']],
                            'remark' => ['value' => '当前库存已经不足，为避免影响正常使用，请及时补充库存'],
                        ]
                    ];
                    
                    if($mainInfo['openid'] == $sInfo['openid']) {
                        if(!empty($mainInfo['openid'])) {
                            //主账号接收消息
                            SELF::pushMsg(
                                $mainInfo['openid'],
                                $msgContent
                                );
                        }
                    }
                    else {
                        if(!empty($mainInfo['openid'])) {
                            //主账号接收消息
                            SELF::pushMsg(
                                $mainInfo['openid'],
                                $msgContent
                                );
                        }
                    
                        if(!empty($sInfo['openid'])) {
                            //操作人接收消息
                            SELF::pushMsg(
                                $sInfo['openid'],
                                $msgContent
                                );
                        }
                    }
                } 
                                
                    if($i==1 && $info['in_storage_num']-$data['num'] != 0 && $info['in_storage_num']-$data['num'] <= $info['alert_num']){//出库时，判断预警数
                        
                        //个推-主账号
                        GeTuiService::push(
                            '库存预警',
                            '仓库：'.$storageName.
                            '中商品：'.$materielName.'-'.
                            $typeName.'剩余库存数达到警戒值，当前剩余库存数：'.
                            $inStorageNum.'为避免影响正常使用，请及时补充库存',
                            $mainInfo['device_id'],
                            );
                        
                        if(!empty($userInfo)){
                            //个推-主账号
                            GeTuiService::push(
                                '库存预警',
                                '仓库：'.$storageName.
                                '中商品：'.$materielName.'-'.
                                $typeName.'剩余库存数达到警戒值，当前剩余库存数：'.
                                $inStorageNum.'为避免影响正常使用，请及时补充库存',
                                $userInfo['device_id'],
                                );
                        }  
                        
                        $msgContent = [
                            'appid' => config('app.wechat_app_id'),
                            'template_id' => config('app.alert_tmpl'),
                            'url' => '',
                            'miniprogram' => '',
                            'data' => [
                                'first' => ['value' => '商品剩余库存已达到警戒值'],
                                'keyword1' => ['value' => $info['materiel_name']],
                                'keyword2' => ['value' => $info['type_name']],
                                'keyword3' => ['value' => $info['in_storage_num']-$data['num']],
                                'remark' => ['value' => '商品库存已达到警戒值，为避免影响正常使用，请及时补充库存']
                            ]
                        ];
                                                              
                        if($mainInfo['openid'] == $sInfo['openid']) {
                            if(!empty($mainInfo['openid'])) {
                                //主账号接收消息
                                SELF::pushMsg(
                                    $mainInfo['openid'],
                                    $msgContent
                                    );
                            }
                        }
                        else {
                            if(!empty($mainInfo['openid'])) {
                                //主账号接收消息
                                SELF::pushMsg(
                                    $mainInfo['openid'],
                                    $msgContent
                                    );
                            }
                        
                            if(!empty($sInfo['openid'])) {
                                //操作人接收消息
                                SELF::pushMsg(
                                    $sInfo['openid'],
                                    $msgContent
                                    );
                            }
                        }
                                        
                    }
                
            
            if($i==1){//出库推送
                                
                if($mainInfo['device_id'] == $sInfo['device_id']) {
                     
                    if(!empty($mainInfo['device_id'])) {
                        //个推-出库操作人
                        GeTuiService::push(
                            '出库提醒',
                            '仓库：'.$storageName.
                            '中商品：'.$materielName.'-'.
                            $typeName.'已成功出库，出库数量:'.$num,
                            $mainInfo['device_id'],
                            );
                    }
                
                    if(!empty($userInfo['device_id'])) {
                        //个推-出库操作人
                        GeTuiService::push(
                            '出库提醒',
                            '仓库：'.$storageName.
                            '中商品：'.$materielName.'-'.
                            $typeName.'已成功出库，出库数量:'.$num,
                            $userInfo['device_id'],
                            );
                    }
                }
                else {
                
                    if(!empty($mainInfo['device_id'])) {
                        //个推-出库操作人
                        GeTuiService::push(
                            '出库提醒',
                            '仓库：'.$storageName.
                            '中商品：'.$materielName.'-'.
                            $typeName.'已成功出库，出库数量:'.$num,
                            $mainInfo['device_id'],
                            );
                    }
                
                
                    if( !empty($userInfo) && $userInfo['device_id'] == $sInfo['device_id']){
                
                        if(!empty($userInfo['device_id'])) {
                            //个推-仓库负责人
                            GeTuiService::push(
                                '出库提醒',
                                '仓库：'.$storageName.
                                '中商品：'.$materielName.'-'.
                                $typeName.'已成功出库，出库数量:'.$num,
                                $userInfo['device_id'],
                                );
                             
                        }
                    }else{
                
                        if(!empty($sInfo['device_id'])) {
                            //个推-仓库负责人
                            GeTuiService::push(
                                '出库提醒',
                                '仓库：'.$storageName.
                                '中商品：'.$materielName.'-'.
                                $typeName.'已成功出库，出库数量:'.$num,
                                $sInfo['device_id'],
                                );
                             
                        }
                
                        if(!empty($userInfo['device_id'])) {
                            //个推-仓库负责人
                            GeTuiService::push(
                                '出库提醒',
                                '仓库：'.$storageName.
                                '中商品：'.$materielName.'-'.
                                $typeName.'已成功出库，出库数量:'.$num,
                                $userInfo['device_id'],
                                );
                             
                        }
                         
                    }
                }
               
                $msgContent = [
                    'appid' => config('app.wechat_app_id'),
                    'template_id' => config('app.out_tmpl'),
                    'url' => '',
                    'miniprogram' => '',
                    'data' => [
                        'first' => ['value' => '货物:'.$info['materiel_name'].'-'.$info['materiel_name'].'        出库数量:'.$data['num']],
                        'keyword1' => ['value' => date('Y-m-d H:i:s')],
                        'keyword2' => ['value' => $info['storage_name']],
                        'remark' => ['value' => '感谢您的使用,请注意库存实时变化'],
                    ]
                ];
               
                if($mainInfo['openid'] == $sInfo['openid']) {
                    
                    if(!empty($mainInfo['openid'])) {
                        //操作人接收消息
                        SELF::pushMsg(
                            $mainInfo['openid'],
                            $msgContent
                            );
                    }
                }
                else {
                    
                    if(!empty($mainInfo['openid'])) {
                        //操作人接收消息
                        SELF::pushMsg(
                            $mainInfo['openid'],
                            $msgContent
                            );
                    }
                
                    if(!empty($sInfo['openid'])) {
                        //仓库负责人接收消息
                        SELF::pushMsg(
                            $sInfo['openid'],
                            $msgContent
                            );
                    }
                }
                
                return true;
                }
            }
          
    /**
     * 出库提醒
     * 仓库负责人和操作人可接收该消息提醒
     * @param $materielName 商品名称
     * @param $typeName 商品类型名称
     * @param $inStorageNum 当前在库数量
     * @param $storageName 仓库名
     * @param $num 操作数量
     * @param $userId 子账号id
     * @param $storageId 仓库id
     * @param $alertNum 低库存预警数
     * @return bool
     */
    static public function sendOutSms($materielName, $typeName, $inStorageNum, $storageName, $num, $data, $storageId, $alertNum)
    {        
        //$adminInfo = AdminModel::where('id', $userId)->first();
        
        if($data['main_id'] == $data['user_id']){
            $userInfo = [];
            $adminInfo = AdminModel::where('id', $data['main_id'])->first();
        
        }else{
            $adminInfo = AdminModel::where('id', $data['main_id'])->first();
            $userInfo = AdminModel::where('id', $data['user_id'])->first();
        }

        /* if($inStorageNum >= $alertNum) { */
            $storageManagerInfo = StorageModel::select(
                'mobile',
                'openid',
                'device_id'
            )
                ->from('storage as a')
                ->leftJoin('admin as b', 'b.id', '=', 'a.user_id')
                ->where('a.id', $storageId)
                ->first();

                if($adminInfo['device_id'] == $storageManagerInfo['device_id']) {
                     
                    if(!empty($adminInfo['device_id'])) {
                        //个推-出库操作人
                        GeTuiService::push(
                            '出库提醒',
                            '仓库：'.$storageName.
                            '中商品：'.$materielName.'-'.
                            $typeName.'已成功出库，出库数量:'.$num,
                            $adminInfo['device_id'],
                            );
                    }
                
                    if(!empty($userInfo['device_id'])) {
                        //个推-出库操作人
                        GeTuiService::push(
                            '出库提醒',
                            '仓库：'.$storageName.
                            '中商品：'.$materielName.'-'.
                            $typeName.'已成功出库，出库数量:'.$num,
                            $userInfo['device_id'],
                            );
                    }
                }else {
                
                    if(!empty($adminInfo['device_id'])) {
                        //个推-出库操作人
                        GeTuiService::push(
                            '出库提醒',
                            '仓库：'.$storageName.
                            '中商品：'.$materielName.'-'.
                            $typeName.'已成功出库，出库数量:'.$num,
                            $adminInfo['device_id'],
                            );
                    }
                
                
                    if( !empty($userInfo) && $userInfo['device_id'] == $storageManagerInfo['device_id']){
                
                        if(!empty($userInfo['device_id'])) {
                            //个推-仓库负责人
                            GeTuiService::push(
                                '出库提醒',
                                '仓库：'.$storageName.
                                '中商品：'.$materielName.'-'.
                                $typeName.'已成功出库，出库数量:'.$num,
                                $userInfo['device_id'],
                                );                             
                        }
                    }else{
                
                        if(!empty($storageManagerInfo['device_id'])) {
                            //个推-仓库负责人
                            GeTuiService::push(
                                '出库提醒',
                                '仓库：'.$storageName.
                                '中商品：'.$materielName.'-'.
                                $typeName.'已成功出库，出库数量:'.$num,
                                $storageManagerInfo['device_id'],
                                );
                             
                        }
                
                        if(!empty($userInfo['device_id'])) {
                            //个推-仓库负责人
                            GeTuiService::push(
                                '出库提醒',
                                '仓库：'.$storageName.
                                '中商品：'.$materielName.'-'.
                                $typeName.'已成功出库，出库数量:'.$num,
                                $userInfo['device_id'],
                                );                             
                        }                         
                    }
                }

            $msgContent = [
                'appid' => config('app.wechat_app_id'),
                'template_id' => config('app.out_tmpl'),
                'url' => '',
                'miniprogram' => '',
                'data' => [
                    'first' => ['value' => '货物:'.$materielName.'-'.$materielName.'        出库数量:'.$num],
                    'keyword1' => ['value' => date('Y-m-d H:i:s')],
                    'keyword2' => ['value' => $storageName],
                    'remark' => ['value' => '感谢您的使用,请注意库存实时变化'],
                ]
            ];

            if($adminInfo['openid'] == $storageManagerInfo['openid']) {
                
                if(!empty($adminInfo['openid'])) {
                    //操作人接收消息
                    SELF::pushMsg(
                        $adminInfo['openid'],
                        $msgContent,
                    );
                }
                
                if(!empty($userInfo['openid'])) {
                    //操作人接收消息
                    SELF::pushMsg(
                        $userInfo['openid'],
                        $msgContent,
                        );
                }
            }else {
                if(!empty($adminInfo['openid'])) {
                    //操作人接收消息
                    SELF::pushMsg(
                        $adminInfo['openid'],
                        $msgContent,
                    );
                }
                if( !empty($userInfo) && $userInfo['device_id'] == $storageManagerInfo['openid']){
                    
                    if(!empty($userInfo['openid'])) {
                        //仓库负责人接收消息
                        SELF::pushMsg(
                            $userInfo['openid'],
                            $msgContent,
                            );
                    }
                }else{
                    if(!empty($storageManagerInfo['openid'])) {
                        //仓库负责人接收消息
                        SELF::pushMsg(
                            $storageManagerInfo['openid'],
                            $msgContent,
                        );
                    }
                
                    if(!empty($userInfo['openid'])) {
                        //仓库负责人接收消息
                        SELF::pushMsg(
                            $userInfo['openid'],
                            $msgContent,
                            );
                    }
                }
            }
        /* } */

        return true;
    }

    /**
     * 库存不足提醒
     * 主账号以及仓库负责人可接收该消息提醒
     * @param $materielName 商品名称
     * @param $typeName 商品类型名称
     * @param $inStorageNum 当前在库数量
     * @param $mainId 主账号id
     * @param $storageName 仓库名
     * @param $storageId 仓库id
     * @return bool
     */
    static public function sendShortSms($materielName, $typeName, $inStorageNum, $data, $storageName, $storageId)
    {        
        //$mainInfo = AdminModel::where('id', $mainId)->first();
        if($data['main_id'] == $data['user_id']){
            $userInfo = [];
            $mainInfo = AdminModel::where('id', $data['main_id'])->first();
        
        }else{
            $mainInfo = AdminModel::where('id', $data['main_id'])->first();
            $userInfo = AdminModel::where('id', $data['user_id'])->first();
        }
       
        $storageManagerInfo = StorageModel::select(
            'mobile',
            'openid',
            'device_id'
        )
            ->from('storage as a')
            ->leftJoin('admin as b', 'b.id', '=', 'a.user_id')
            ->where('a.id', $storageId)
            ->first();

        if($inStorageNum == 0) {
           
//            $info = [
//                'storage' => $storageName,
//                'materiel' => $materielName.'-'.$typeName,
//                'num' => $inStorageNum
//            ];

//            if(!empty($storageManagerInfo['mobile'])) {
//                if($mainInfo['mobile'] == $storageManagerInfo['mobile']) {
//                    //短信-推送至主账号
//                    SmsService::sendStorageAlertMsg(
//                        $info,
//                        $mainInfo['mobile'],
//                        config('app.short_template_id')
//                    );
//                }
//                else {
//                    //短信-推送至主账号
//                    SmsService::sendStorageAlertMsg(
//                        $info,
//                        $mainInfo['mobile'],
//                        config('app.short_template_id')
//                    );
//
//                    //短信-推送至仓库负责人
//                    SmsService::sendStorageAlertMsg(
//                        $info,
//                        $storageManagerInfo['mobile'],
//                        config('app.short_template_id')
//                    );
//                }
//            }
//            else {
//                //短信-推送至主账号
//                SmsService::sendStorageAlertMsg(
//                    $info,
//                    $mainInfo['mobile'],
//                    config('app.short_template_id')
//                );
//            }


            if($mainInfo['device_id'] == $storageManagerInfo['device_id']) {
                if(!empty($mainInfo['device_id'])) {
                    //个推-主账号
                    GeTuiService::push(
                        '库存不足',
                        '仓库：'.$storageName.
                        '中商品：'.$materielName.'-'.
                        $typeName.'剩余库存数不足，当前剩余库存数：'.
                        $inStorageNum.'为避免影响使用，请及时补充库存',
                        $mainInfo['device_id'],
                    );
                }
                
                if(!empty($userInfo['device_id'])){
                    //个推-操作
                    GeTuiService::push(
                        '库存不足',
                        '仓库：'.$storageName.
                        '中商品：'.$materielName.'-'.
                        $typeName.'剩余库存数不足，当前剩余库存数：'.
                        $inStorageNum.'为避免影响使用，请及时补充库存',
                        $userInfo['device_id'],
                        );
                }
            }
            else {
                if(!empty($mainInfo['device_id'])) {
                    //个推-主账号
                    GeTuiService::push(
                        '库存不足',
                        '仓库：'.$storageName.
                        '中商品：'.$materielName.'-'.
                        $typeName.'剩余库存数不足，当前剩余库存数：'.
                        $inStorageNum.'为避免影响使用，请及时补充库存',
                        $mainInfo['device_id'],
                    );
                }

                if( !empty($userInfo) && $userInfo['device_id'] == $storageManagerInfo['device_id']){
                
                    if(!empty($userInfo['device_id'])){
                        //个推-操作
                        GeTuiService::push(
                            '库存不足',
                            '仓库：'.$storageName.
                            '中商品：'.$materielName.'-'.
                            $typeName.'剩余库存数不足，当前剩余库存数：'.
                            $inStorageNum.'为避免影响使用，请及时补充库存',
                            $userInfo['device_id'],
                            );
                    }
                }else{
                
                    if(!empty($storageManagerInfo['device_id'])) {
                        //个推-仓库负责人
                        GeTuiService::push(
                            '库存不足',
                            '仓库：'.$storageName.
                            '中商品：'.$materielName.'-'.
                            $typeName.'剩余库存数不足，当前剩余库存数：'.
                            $inStorageNum.'为避免影响使用，请及时补充库存',
                            $storageManagerInfo['device_id'],
                            );
                    }
                
                    if(!empty($userInfo['device_id'])){
                        //个推-操作
                        GeTuiService::push(
                            '库存不足',
                            '仓库：'.$storageName.
                            '中商品：'.$materielName.'-'.
                            $typeName.'剩余库存数不足，当前剩余库存数：'.
                            $inStorageNum.'为避免影响使用，请及时补充库存',
                            $userInfo['device_id'],
                            );
                    }
                }
            }

            $msgContent = [
                'appid' => config('app.wechat_app_id'),
                'template_id' => config('app.short_tmpl'),
                'url' => '',
                'miniprogram' => '',
                'data' => [
                    'first' => ['value' => '库存不足'],
                    'keyword1' => ['value' => $materielName],
                    'keyword2' => ['value' => $typeName],
                    'keyword3' => ['value' => $inStorageNum],
                    'remark' => ['value' => '当前库存已经不足，为避免影响正常使用，请及时补充库存'],
                ]
            ];

            if($mainInfo['openid'] == $storageManagerInfo['openid']) {
           
                if(!empty($mainInfo['openid'])) {
                    //操作人接收消息
                    SELF::pushMsg(
                        $mainInfo['openid'],
                        $msgContent,
                        );
                }
            
                if(!empty($userInfo['openid'])) {
                    //操作人接收消息
                    SELF::pushMsg(
                        $userInfo['openid'],
                        $msgContent,
                        );
                }
            }else {
               
                if(!empty($mainInfo['openid'])) {
                    //操作人接收消息
                    SELF::pushMsg(
                        $mainInfo['openid'],
                        $msgContent,
                        );
                }
                if(!empty($userInfo) && $userInfo['openid'] == $storageManagerInfo['openid']){
            
                    if(!empty($userInfo['openid'])) {
                        //仓库负责人接收消息
                        SELF::pushMsg(
                            $userInfo['openid'],
                            $msgContent,
                            );
                    }
                }else{
                    if(!empty($storageManagerInfo['openid'])) {
                        //仓库负责人接收消息
                        SELF::pushMsg(
                            $storageManagerInfo['openid'],
                            $msgContent,
                            );
                    }
            
                    if(!empty($userInfo['openid'])) {
                        //仓库负责人接收消息
                        SELF::pushMsg(
                            $userInfo['openid'],
                            $msgContent,
                            );
                    }
                }
            }
        }
        return true;
    }

    /**
     * 入库提醒
     * 仅仓库负责人和操作人可接收该消息
     * @param $materielName
     * @param $typeName
     * @param $storageId
     * @param $storageName
     * @param $userId
     * @param $num 操作数量
     * @param $inStorageNum
     * @return bool
     */
    static public function sendInSms($purchaseNo, $materielName, $typeName, $storageId, $storageName, $data, $num, $inStorageNum)
    {
        /* $adminInfo = AdminModel::where('id', $userId)->first(); */
        
        if($data['main_id'] == $data['user_id']){
            $userInfo = [];
            $adminInfo = AdminModel::where('id', $data['main_id'])->first();
        
        }else{
            $adminInfo = AdminModel::where('id', $data['main_id'])->first();
            $userInfo = AdminModel::where('id', $data['user_id'])->first();
        }
        

        $storageManagerInfo = StorageModel::select(
            'mobile',
            'openid',
            'device_id'
        )
            ->from('storage as a')
            ->leftJoin('admin as b', 'b.id', '=', 'a.user_id')
            ->where('a.id', $storageId)
            ->first();


            if($adminInfo['device_id'] == $storageManagerInfo['device_id']) {
                if(!empty($adminInfo['device_id'])) {
                    //个推-主账号
                    GeTuiService::push(
                        '入库提醒',
                        '商品：'.$materielName.'-'.
                        $typeName.'已成功入库至仓库:'.$storageName.'，当前剩余库存数：'.
                        $inStorageNum.'入库操作数量:'.$num,
                        $adminInfo['device_id'],
                        );
                }
            
                if(!empty($userInfo['device_id'])) {
                    //个推-操作人
                    GeTuiService::push(
                        '入库提醒',
                        '商品：'.$materielName.'-'.
                        $typeName.'已成功入库至仓库:'.$storageName.'，当前剩余库存数：'.
                        $inStorageNum.'入库操作数量:'.$num,
                        $userInfo['device_id'],
                        );
                }
            }else {
                if(!empty($adminInfo['device_id'])) {
                    //个推-主账号
                    GeTuiService::push(
                        '入库提醒',
                        '商品：'.$materielName.'-'.
                        $typeName.'已成功入库至仓库:'.$storageName.'，当前剩余库存数：'.
                        $inStorageNum.'入库操作数量:'.$num,
                        $adminInfo['device_id'],
                        );
                }
            
                if( !empty($userInfo) && $userInfo['device_id'] == $storageManagerInfo['device_id']){
                    if(!empty($userInfo['device_id'])) {
                        //个推-操作人
                        GeTuiService::push(
                            '入库提醒',
                            '商品：'.$materielName.'-'.
                            $typeName.'已成功入库至仓库:'.$storageName.'，当前剩余库存数：'.
                            $inStorageNum.'入库操作数量:'.$num,
                            $userInfo['device_id'],
                            );
                    }
                }else{
                    if(!empty($storageManagerInfo['device_id'])) {
                        //个推-主账号
                        GeTuiService::push(
                            '入库提醒',
                            '商品：'.$materielName.'-'.
                            $typeName.'已成功入库至仓库:'.$storageName.'，当前剩余库存数：'.
                            $inStorageNum.'入库操作数量:'.$num,
                            $storageManagerInfo['device_id'],
                            );
                    }
            
                    if(!empty($userInfo['device_id'])) {
                        //个推-操作人
                        GeTuiService::push(
                            '入库提醒',
                            '商品：'.$materielName.'-'.
                            $typeName.'已成功入库至仓库:'.$storageName.'，当前剩余库存数：'.
                            $inStorageNum.'入库操作数量:'.$num,
                            $userInfo['device_id'],
                            );
                    }
                }
            }

        $msgContent = [
            'appid' => config('app.wechat_app_id'),
            'template_id' => config('app.in_tmpl'),
            'url' => '',
            'miniprogram' => '',
            'data' => [
                'first' => ['value' => '已入库完成'],
                'keyword1' => ['value' => $purchaseNo],
                'keyword2' => ['value' => $storageName],
                'keyword3' => ['value' => $materielName.'-'.
                    $typeName],
                'keyword4' => ['value' => $num],
                'keyword5' => ['value' => date('Y-m-d H:i:s')],
                'remark' => ['value' => ''],
            ]
        ];

        if($adminInfo['openid'] == $storageManagerInfo['openid']) {
            if(!empty($adminInfo['openid'])) {
                //操作人接收消息
                SELF::pushMsg(
                    $adminInfo['openid'],
                    $msgContent,
                );
            }
            
            if(!empty($userInfo['openid'])) {
                //操作人接收消息
                SELF::pushMsg(
                    $userInfo['openid'],
                    $msgContent,
                    );
            }
        }
        else {
            if(!empty($adminInfo['openid'])) {
                //操作人接收消息
                SELF::pushMsg(
                    $adminInfo['openid'],
                    $msgContent,
                );
            }
        if(!empty($userInfo) && $userInfo['openid'] == $storageManagerInfo['openid']){
            if(!empty($userInfo['openid'])) {
                //仓库负责人接收消息
                SELF::pushMsg(
                    $userInfo['openid'],
                    $msgContent,
                    );
            }
        }else{
            if(!empty($storageManagerInfo['openid'])) {
                //仓库负责人接收消息
                SELF::pushMsg(
                    $storageManagerInfo['openid'],
                    $msgContent,
                );
            }
            if(!empty($userInfo['openid'])) {
                //仓库负责人接收消息
                SELF::pushMsg(
                    $userInfo['openid'],
                    $msgContent,
                    );
            }
           }
        }

        return true;
    }

    /**
     * 低库存预警消息提醒
     * 主账号接收预警提醒；仓库负责人接收出库提醒
     * @param $materielName 商品名称
     * @param $typeName 商品类型名称
     * @param $inStorageNum 当前在库数量
     * @param $alertNum 低库存预警数
     * @param $mainId 主账号id
     * @param $storageName 仓库名
     * @return bool
     */
    static public function sendAlertSms($materielName, $typeName, $inStorageNum, $alertNum, $data, $storageId, $storageName, $num)
    {
        
        //$mainInfo = AdminModel::where('id', $mainId)->first();
        
        if($data['main_id'] == $data['user_id']){
            $userInfo = [];
            $mainInfo = AdminModel::where('id', $data['main_id'])->first();
        
        }else{
            $mainInfo = AdminModel::where('id', $data['main_id'])->first();
            $userInfo = AdminModel::where('id', $data['user_id'])->first();
        }

        $storageManagerInfo = StorageModel::select(
            'mobile',
            'openid',
            'device_id'
        )
            ->from('storage as a')
            ->leftJoin('admin as b', 'b.id', '=', 'a.user_id')
            ->where('a.id', $storageId)
            ->first();

        $info = [
            'storage' => $storageName,
            'materiel' => $materielName.'-'.$typeName,
            'num' => $inStorageNum
        ];

        //库存数达到预警
        if($inStorageNum != 0 && $inStorageNum <= $alertNum) {
         /*    SmsService::sendStorageAlertMsg(
                $info,
                $mainInfo['mobile'],
                config('app.alert_template_id')
            ); */

            //个推-主账号
            GeTuiService::push(
                '库存预警',
                '仓库：'.$storageName.
                '中商品：'.$materielName.'-'.
                $typeName.'剩余库存数达到警戒值，当前剩余库存数：'.
                $inStorageNum.'为避免影响正常使用，请及时补充库存',
                $mainInfo['device_id'],
            );
            if(!empty($userInfo)){
                //个推-主账号
                GeTuiService::push(
                    '库存预警',
                    '仓库：'.$storageName.
                    '中商品：'.$materielName.'-'.
                    $typeName.'剩余库存数达到警戒值，当前剩余库存数：'.
                    $inStorageNum.'为避免影响正常使用，请及时补充库存',
                    $userInfo['device_id'],
                    );
            }
            $msgContent = [
                'appid' => config('app.wechat_app_id'),
                'template_id' => config('app.alert_tmpl'),
                'url' => '',
                'miniprogram' => '',
                'data' => [
                    'first' => ['value' => '商品剩余库存已达到警戒值'],
                    'keyword1' => ['value' => $materielName],
                    'keyword2' => ['value' => $typeName],
                    'keyword3' => ['value' => $inStorageNum],
                    'remark' => ['value' => '商品库存已达到警戒值，为避免影响正常使用，请及时补充库存'],
                ]
            ];

           if($mainInfo['openid'] == $storageManagerInfo['openid']) {
                if(!empty($mainInfo['openid'])) {
                    //主账号接收消息
                    SELF::pushMsg(
                        $mainInfo['openid'],
                        $msgContent
                        );
                }
                
                if(!empty($userInfo['openid'])) {
                    //主账号接收消息
                    SELF::pushMsg(
                        $userInfo['openid'],
                        $msgContent
                        );
                }
            }else {
                if(!empty($mainInfo['openid'])) {
                    //主账号接收消息
                    SELF::pushMsg(
                        $mainInfo['openid'],
                        $msgContent
                        );
                }
            if(!empty($userInfo) && $userInfo['openid'] == $storageManagerInfo['openid']){
                if(!empty($userInfo['openid'])) {
                    //主账号接收消息
                    SELF::pushMsg(
                        $userInfo['openid'],
                        $msgContent
                        );
                }
            }else{
                if(!empty($storageManagerInfo['openid'])) {
                    //操作人接收消息
                    SELF::pushMsg(
                        $storageManagerInfo['openid'],
                        $msgContent
                        );
                } 
                if(!empty($userInfo['openid'])) {
                    //主账号接收消息
                    SELF::pushMsg(
                        $userInfo['openid'],
                        $msgContent
                        );
                }
            }
            }         
        }

        return true;
    }

    static private function pushMsg($openId, $msgContent)
    {    
        $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=".SELF::getAccessToken();
        $data = [
            'access_token' => SELF::getAccessToken(),
            'touser' => $openId,
            'mp_template_msg' => $msgContent,
        ];

        $result = SELF::httpPostData($url, $data);
       
        Log::debug('出库:'.var_export($result, true));

        return true;
    }

    /**发起网络请求  获取 access_token */
    static private function getAccessToken(){
        if(!Redis::exists('wechat_token')) {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".config('app.miniapp_id')."&secret=".config('app.miniapp_secret');
            $info = file_get_contents($url);
            $json = json_decode($info);
            $arr = get_object_vars($json);

            Redis::setex('wechat_token', 7200, $arr['access_token']);
        }

        return Redis::get('wechat_token');
    }

    /**http - post请求*/
    static private function httpPostData($url, $data)
    {
        $data = json_encode($data);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($data)
        ));
        $returnContent = curl_exec($ch);
        return $returnContent;
    }
}
